<template>
	<view>
		<view class="uniFlex">
			<view>现有金额：<text style="color: #FC6CA7;">598.54</text></view>
		</view>
		
		<view class="uniFlex">
			<view>充值金额：</view>
			<input type="number" v-model="money" placeholder-class="pcs" placeholder="请输入100的倍数">
		</view>
		
		<view @click="submit" class="other-button" hover-class="other-button-hover">去充值</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				money:''
			}
		},
		onLoad() {
			
		},
		methods: {
			async submit(){
				let reg = /^(?:[1-9]\d00|[1-9]\d{1,}0)$/
				if(!reg.test(this.money)){
					uni.showToast({
						title:'请输入100的倍数',
						icon:'none'
					})
					return;
				}
				let res = await this.$api.chongzhi({
					uid:uni.getStorageSync("tokenUid"),
					paytype: 'jspay',
					openid: uni.getStorageSync("openid"),
					price:this.money
				});
				if (res.code == 200) {
					let payment = res.data;
					uni.requestPayment({
						...payment,
						success(res) {
							uni.showToast({
								title:'充值成功'
							})
							setTimeout(function(){
								uni.reLaunch({
									url:'/pages/main/main'
								})
							},1000)
						},
						fail(err) {
							console.error('pay fail', err)
							uni.showToast({
								title: "支付失败",
								icon: "none"
							})
							setTimeout(function(){
								uni.navigateBack()
							},1000)
						}
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	page{
		background: #fff;
	}
	.uniFlex{
		margin: 30upx;
		border-bottom: 1px solid #eee;
		padding: 15upx 0;
		
		input{
			flex: 1;
		}
	}

</style>
